﻿@using ERP.WebUI
@model RoleSelectedModuleModel
@{
    int sortOrder = 1;
}

<div id="content-body" class="row-fluid">
    <div class="span22">
        <div class="widget-box">
            <div class="widget-title">
                <span class="icon"><i class="icon-edit"></i></span>
                <h5>设置权限</h5>
            </div>
            <div class="widget-content nopadding">
                <form id="modal-content" class="form-horizontal search-form">
                    @Html.HiddenFor(t => t.RoleId)
                    @Html.HiddenFor(t => t.RoleName)
                    <input type="hidden" name="isSet" value="0" />

                    <!-- Nav tabs -->
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#" data-target='#module' data-toggle="tab">模块菜单</a></li>
                        <li><a href="@Url.Action("GetPermission")" data-target='#permission' data-toggle="tab">按钮权限</a></li>
                    </ul>

                    <!-- Tab panes -->
                    <div class="tab-content">
                        <div class="tab-pane active" id="module">
                            <table class="module-tbl table table-bordered data-table dataTables_wrapper">
                                <thead>
                                    <tr>
                                        <th width="10%">序号</th>
                                        <th width="12%">
                                            <input type="checkbox" onclick="CheckAllModule(this.checked)" />
                                        </th>
                                        <th width="30%">模块(菜单)名称</th>
                                        <th width="48%">模块编码</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.ModuleDataList)
                                    {
                                        <tr>
                                            <td>@(sortOrder++)</td>

                                            @if (item.ParentId != null)
                                            {
                                                <td>@Html.CheckBox("modulecheck", item.Selected, new { @class = "modulecheck childmodulecheck-" + item.ParentId, @value = item.ModuleId, onclick = "CheckChild(" + item.ParentId + ")" })</td>
                                                <td style="text-align: right;">[ @item.ModuleName ]</td>
                                            }
                                            else
                                            {
                                                <td>@Html.CheckBox("modulecheck", item.Selected, new { @class = "modulecheck parentmodulecheck-" + item.ModuleId, @value = item.ModuleId, onclick = "CheckParent(this.checked, " + item.ModuleId + ")" })</td>
                                                <td>@item.ModuleName</td>
                                            }
                                            <td>@item.Code</td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                        <div class="tab-pane" id="permission">
                        </div>
                    </div>
                    <div class="form-actions form-operate">
                        <input type="submit" value="确定" class="btn btn-primary" id="model-submit" onclick="SavePermission()">
                        <input type="button" value="返回" onclick="history.back(-1)" class="btn btn-primary">
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@Html.ScriptBlock(
    @<script type="text/javascript">
         $(function () {
             //Ajax Tab
             $('[data-toggle="tab"]').click(function (e) {
                 e.preventDefault();
                 var loadurl = $(this).attr('href');
                 var targ = $(this).attr('data-target');
                 if (targ == "#permission") {
                     //Set Permission
                     $("input[name='isSet']").val(1);

                     //Role Id
                     var roleId = $("#RoleId").val();

                     //Module Id
                     var newModuleIds = new Array();
                     $(".modulecheck").each(function () {
                         if (this.checked) {
                             newModuleIds.push($(this).val());
                         }
                     });

                     //Data
                     var data = { RoleId: roleId, selectedModules: newModuleIds.join(",") };

                     //Get Permission
                     $.get(loadurl, data, function (result) {
                         $(targ).html(result);
                     });
                 } else {
                     $("#permission").html();
                 }
                 $(this).tab('show');
             });
         })

         //模块 全选/反选
         function CheckAllModule(val) {
             $("input[name='modulecheck']").each(function () {
                 this.checked = val;
             });
         }

         //父模块 选中
         function CheckParent(val, Id) {
             $(".childmodulecheck-" + Id).each(function () {
                 this.checked = val;
             });
         }

         //子模块 选中
         function CheckChild(Id) {
             var checkedCount = 0;
             $(".childmodulecheck-" + Id).each(function () {
                 if (this.checked) {
                     checkedCount = checkedCount + 1;
                 }
             });
             if (checkedCount != 0) {
                 $(".parentmodulecheck-" + Id).attr("checked", true);
             } else {
                 $(".parentmodulecheck-" + Id).attr("checked", false);
             }
         }

         //权限 全选/反选
         function CheckAllPermission(val) {
             $("input[name='permissioncheck'],input[name='modulectx']").each(function () {
                 this.checked = val;
             });
         }

         //父模块 选中
         function CheckParentModulePermission(val, Id) {
             $(".module-" + Id).each(function () {
                 this.checked = val;
             });
         }

         //TODO 父权限 选中 
         function CheckParentPermission(val, Id, PId) {
             var checkedCount = 0;
             $(".childpermissioncheck-" + Id).each(function () {
                 this.checked = val;
             });
             $(".module-" + PId).each(function () {
                 if (this.checked) {
                     checkedCount = checkedCount + 1;
                 }
             });
             if (checkedCount != 1) {
                 $(".parentallpermissioncheck-" + PId).attr("checked", true);
             } else {
                 $(".parentallpermissioncheck-" + PId).attr("checked", false);
             }
         }

         //子权限 选中
         function CheckChildPermission(Id, PId) {
             var checkedCount = 0;
             $(".childpermissioncheck-" + Id).each(function () {
                 if (this.checked) {
                     checkedCount = checkedCount + 1;
                 }
             });
             if (checkedCount != 0) {
                 $(".parentpermissioncheck-" + Id).attr("checked", true);
             } else {
                 $(".parentpermissioncheck-" + Id).attr("checked", false);
             }

             var checkedParentCount = 0;
             $(".module-" + PId).each(function () {
                 if (this.checked) {
                     checkedParentCount = checkedParentCount + 1;
                 }
             });
             if (checkedParentCount != 1) {
                 $(".parentallpermissioncheck-" + PId).attr("checked", true);
             } else {
                 $(".parentallpermissioncheck-" + PId).attr("checked", false);
             }
         }

         //保存授权
         function SavePermission() {
             //新模块权限
             var roleId = $("#RoleId").val();
             var newModulePermission = new Array();
             $("input[type = 'checkbox'][name = 'modulectx']").each(function () {
                 if (this.checked) {
                     //模块
                     var moduleId = $(this).val();
                     //权限列表
                     var permissionObj = $(".childpermissioncheck-" + $(this).val());
                     if (permissionObj.length == 0) {
                         //父模块
                         var newModulePermissionData = {};
                         newModulePermissionData["RoleId"] = roleId;
                         newModulePermissionData["ModuleId"] = moduleId;
                         newModulePermissionData["PermissionId"] = null;
                         newModulePermission.push(newModulePermissionData);
                     } else {
                         permissionObj.each(function () {
                             if (this.checked) {
                                 var newModulePermissionData = {};
                                 newModulePermissionData["RoleId"] = roleId;
                                 newModulePermissionData["ModuleId"] = moduleId;
                                 newModulePermissionData["PermissionId"] = $(this).val();
                                 //添加到数组
                                 newModulePermission.push(newModulePermissionData);
                             }
                         });
                     }
                 }
             });

             var actionUrl = "@Url.Action("SetPermission")";
             var $form = $("#modal-content");
             var isSet = $("input[name='isSet']").val();
             var data = { RoleId: roleId, IsSet: isSet, newModulePermission: JSON.stringify(newModulePermission) }
             $.ajax({
                 type: "POST",
                 url: actionUrl,
                 data: data,
                 success: function (data) {
                     //判断返回值，若为Object类型，即操作成功
                     var result = ((typeof data == 'object') && (data.constructor == Object));
                     alertMessage(1, data.Message, 5);
                     var url = "@Url.Action("Index")";
                     location.href = url;
                 }
             });
         }
    </script>
    )
